package tech.runchen.gz5055.oms.modules.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import tech.runchen.gz5055.oms.common.utils.Result;
import tech.runchen.gz5055.oms.common.utils.StringUtils;
import tech.runchen.gz5055.oms.common.utils.file.FileUploadUtils;
import tech.runchen.gz5055.oms.common.utils.file.FileUtils;
import tech.runchen.gz5055.oms.config.ServerConfig;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zzj
 */
@Api(tags = "管理平台 - 公共接口")
@RestController
@RequestMapping("/sys/common")
public class SysCommonController {

    @Autowired
    private ServerConfig serverConfig;

    @ApiOperation("通用上传请求（单个）")
    @PostMapping("/upload")
    public Result uploadFile(MultipartFile file) {
        try {
            String originalFilename = file.getOriginalFilename();
            String fileName = FileUploadUtils.upload(FileUploadUtils.PROFILE_PATH + "/upload", file);
            String url = serverConfig.getUrl() + fileName;
            return Result.success().put("url", url).put("fileName", fileName).put("originalFilename", originalFilename).put("md5", FileUtils.getMd5(file));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation("通用上传请求（多个）")
    @PostMapping("/uploads")
    public Result uploadFiles(List<MultipartFile> files) {
        try {
            // 上传文件路径
            List<String> urls = new ArrayList<String>();
            List<String> fileNames = new ArrayList<String>();
            List<String> originalFilenames = new ArrayList<String>();
            List<String> fileMD5s = new ArrayList<String>();
            for (MultipartFile file : files) {
                // 上传并返回新文件名称
                String originalFilename = file.getOriginalFilename();
                String fileName = FileUploadUtils.upload(FileUploadUtils.PROFILE_PATH + "/upload", file);
                String url = serverConfig.getUrl() + fileName;
                urls.add(url);
                fileNames.add(fileName);
                originalFilenames.add(originalFilename);
                fileMD5s.add(FileUtils.getMd5(file));
            }
            Result ajax = Result.success();
            ajax.put("urls", StringUtils.join(urls, ","));
            ajax.put("fileNames", StringUtils.join(fileNames, ","));
            ajax.put("originalFilenames", StringUtils.join(originalFilenames, ","));
            ajax.put("md5s", StringUtils.join(fileMD5s, ","));
            return ajax;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }


}
